package com.qdxwx.manage.controller.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户(课程学员)Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/user")
public class CourseUserController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/list.html")
    public String user(HttpServletRequest request, ModelMap mmap) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        mmap.put("courseId",courseId);
        return "course/course-user";
    }

    /**
     * 查询用户(我的学员)列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if(courseId < 1) return TableDataInfo.error("课程id有误");
        List<User> list = userService.getStudentList(courseId, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(userService.countStudent(courseId));
        return rspData;
    }

}
